<?php

namespace YmPhp\Admin\Http\Controllers\Business;

use YmPhp\Admin\Admin;
use YmPhp\Admin\Layout\Column;
use YmPhp\Admin\Models\Business\BusinessCategory;
use YmPhp\Admin\Form;
use YmPhp\Admin\Form\Tab;
use YmPhp\Admin\Widgets\Tooltip;
use YmPhp\Admin\Http\Controllers\AdminController;
use YmPhp\Admin\Layout\Row;
use YmPhp\Admin\Layout\Content;
use YmPhp\Admin\Tree;
use YmPhp\Admin\Widgets\Box;
use YmPhp\Admin\Widgets\Form as WidgetForm;
use YmPhp\Admin\Http\Actions\Business\CategoryShow;

class BusinessCategoryController extends AdminController
{

    public function index(Content $content)
    {
        return $content->header('商家类目')
            ->body(function (Row $row) {
                $tree = new Tree(new BusinessCategory);
                $tree->disableCreateButton();
                $tree->disableQuickCreateButton();
                $tree->disableEditButton();
                $tree->expand(false);
                $tree->actions(function (Tree\Actions $actions) {
                    if ($actions->getRow()->extension) {
                        $actions->disableDelete();
                    }
                    Tooltip::make('.icon-eye')->title(trans('business-category.fields.hide_index'))->background('#414750')->right();
                    Tooltip::make('.icon-eye-off')->title( trans('business-category.fields.hide_index'))->background('#edc30e')->right();
                    Tooltip::make('.icon-trash')->title(trans('business-category.del'))->background('#f56c6c')->top();
                    Tooltip::make('.icon-edit')->title(trans('business-category.edit'))->left();
                    $actions->prepend(new CategoryShow());
                });
                $tree->branch(function ($branch) {
                    Admin::style("
                        .dd-handle{
                            cursor: pointer;
                        }

                    ");
                    if ($branch['icon']){
                        $logo = "<img data-action='preview-img' src='{$branch['icon']}' style='max-width:30px;max-height:30px' class='img img-thumbnail'/>";
                    }else{
                        $logo = "<i class='fa fa-folder-o'></i>";
                    }


                    $price = $branch['price'] > 0 ? $branch['price'] : trans('admin.free');
                    return " <span style='font-size: 14px;font-weight: bold'>$logo  {$branch['name']}【{$branch['id']}】 </span> <span style='margin-left: 50px;font-size: 12px'>查看联系方式价格：$price</span>";
                });
                $row->column(7, $tree);
                $row->column(5, function (Column $column) {
                    $form = new WidgetForm();
                    $form->tab(trans('admin.basics'), function ($form) {
                        $form->select('parent_id')->options(BusinessCategory::selectOptions())->required();
                        $form->text('name')->required();
                        $form->image('icon')->move('upload/business/category/')->autoUpload()->uniqueName()->saveFullUrl();
                        $form->currency('price')->default(0)->symbol('￥')->required();
                        $form->text('o')->default(0)->required();
                    });
                    $form->tab(trans('admin.share'), function ($form) {
                        $form->text('share_title');
                        $form->textarea('share_desc')->rows(3);
                        $form->image('share_pic')->move('upload/business/category/')->autoUpload()->uniqueName()->saveFullUrl();
                        $form->text('cat_link');
                    });
                    $form->width(9, 3);
                    $column->append(Box::make(trans('admin.new'), $form));
                });
            });
    }
    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new BusinessCategory(), function (Form $form) {
            $form->select('parent_id')->options(BusinessCategory::selectOptions())->required();
            $form->text('name');
            $form->image('icon')->move('upload/business/category/')->autoUpload()->uniqueName()->saveFullUrl();
            $form->text('aid');
            $form->text('o');
            $form->text('price');
            $form->text('tag');
            $form->text('cat_ids');
            $form->text('stids');
            $form->text('share_title');
            $form->textarea('share_desc')->rows(3);
            $form->image('share_pic')->move('upload/business/category/')->autoUpload()->uniqueName()->saveFullUrl();
            $form->text('cat_link');
            $form->text('hideindex');
            $form->text('end_at');

        });
    }
    /**
     * Help message for icon field.
     *
     * @return string
     */
    protected function iconHelp()
    {
        return 'For more icons please see <a href="http://fontawesome.io/icons/" target="_blank">http://fontawesome.io/icons/</a>';
    }
}
